﻿using SD.Infrastructure.EntityBase;
using SlamDunk.ValueObjects.Enums;
using System;

namespace SlamDunk.MES.Domain.Entities.ResourceContext
{
    /// <summary>
    /// 物料条码设置
    /// </summary>
    public class MaterialBarcodeSetting : PlainEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected MaterialBarcodeSetting() { }
        #endregion

        #region 01.创建物料条码设置构造器
        /// <summary>
        /// 创建物料条码设置构造器
        /// </summary>
        /// <param name="needPrintBarcode">是否打印条码</param>
        /// <param name="barcodeMode">条码模式</param>
        /// <param name="barcodeUnit">条码单位</param>
        public MaterialBarcodeSetting(bool needPrintBarcode, BarcodeMode? barcodeMode, string barcodeUnit)
            : this()
        {
            this.Init(needPrintBarcode, barcodeMode, barcodeUnit);
        }
        #endregion

        #endregion

        #region # 属性

        #region 是否打印条码 —— bool NeedPrintBarcode
        /// <summary>
        /// 是否打印条码
        /// </summary>
        public bool NeedPrintBarcode { get; private set; }
        #endregion

        #region 条码模式 —— BarcodeMode? BarcodeMode
        /// <summary>
        /// 条码模式
        /// </summary>
        public BarcodeMode? BarcodeMode { get; private set; }
        #endregion

        #region 条码单位 —— string BarcodeUnit
        /// <summary>
        /// 条码单位
        /// </summary>
        public string BarcodeUnit { get; private set; }
        #endregion

        #region 外键属性 - 物料定义Id —— Guid MaterialId
        /// <summary>
        /// 外键属性 - 物料定义Id
        /// </summary>
        public Guid MaterialId { get; private set; }
        #endregion

        #region 导航属性 - 物料定义 —— Material Material
        /// <summary>
        /// 导航属性 - 物料定义
        /// </summary>
        public virtual Material Material { get; internal set; }
        #endregion

        #endregion

        #region # 方法

        #region 初始化物料条码设置 —— void Init(bool needPrintBarcode...
        /// <summary>
        /// 初始化物料条码设置
        /// </summary>
        /// <param name="needPrintBarcode">是否打印条码</param>
        /// <param name="barcodeMode">条码模式</param>
        /// <param name="barcodeUnit">条码单位</param>
        public void Init(bool needPrintBarcode, BarcodeMode? barcodeMode, string barcodeUnit)
        {
            #region # 验证

            if (needPrintBarcode && !barcodeMode.HasValue)
            {
                throw new ArgumentNullException(nameof(barcodeMode), "需打印条码必须设置条码模式！");
            }
            if (needPrintBarcode && barcodeMode == ValueObjects.Enums.BarcodeMode.Unique && string.IsNullOrWhiteSpace(barcodeUnit))
            {
                throw new ArgumentNullException(nameof(barcodeMode), "需打印唯一码必须设置条码单位！");
            }

            #endregion

            this.NeedPrintBarcode = needPrintBarcode;
            this.BarcodeMode = barcodeMode;
            this.BarcodeUnit = barcodeUnit;
        }
        #endregion

        #endregion
    }
}
